import router from '@/router/index'
import useUserStore from '@/stores/modeles/user'
import pinia from './stores'
const userStore = useUserStore(pinia)
router.beforeEach(async (to: any, from: any, next: any) => {
  if (userStore.token) {
    if (to.path == '/login') {
      next('/')
    } else {
      if (userStore.username) {
        next()
      } else {
        try {
          await userStore.getUserInfo()
          next({ ...to, replace: true })
        } catch (error) {
          await userStore.getLogout()
          next('/login')
        }
      }
    }
  } else {
    if (to.path == '/login') {
      next()
    } else {
      next({ path: '/login' })
    }
  }
  next()
})
